﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//请先添加引用
namespace Usas.DAL.Cms
{
    /// <summary>
    /// 数据访问类cms_block。
    /// </summary>
    public class cms_block
    {
        public cms_block()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int b_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from cms_block");
            strSql.Append(" where b_id=" + b_id + " ");
            return DbHelperSQL.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Usas.Model.Cms.cms_block model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.b_name != null)
            {
                strSql1.Append("b_name,");
                strSql2.Append("'" + model.b_name + "',");
            }
            if (model.b_path != null)
            {
                strSql1.Append("b_path,");
                strSql2.Append("'" + model.b_path + "',");
            }
            if (model.b_remark != null)
            {
                strSql1.Append("b_remark,");
                strSql2.Append("'" + model.b_remark + "',");
            }
            if (model.b_setting_url != null)
            {
                strSql1.Append("b_setting_url,");
                strSql2.Append("'" + model.b_setting_url + "',");
            }
            strSql.Append("insert into cms_block(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Usas.Model.Cms.cms_block model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update cms_block set ");
            if (model.b_name != null)
            {
                strSql.Append("b_name='" + model.b_name + "',");
            }
            if (model.b_path != null)
            {
                strSql.Append("b_path='" + model.b_path + "',");
            }
            if (model.b_remark != null)
            {
                strSql.Append("b_remark='" + model.b_remark + "',");
            }
            if (model.b_setting_url != null)
            {
                strSql.Append("b_setting_url='" + model.b_setting_url + "',");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where b_id=" + model.b_id + " ");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int b_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from cms_block ");
            strSql.Append(" where b_id=" + b_id + " ");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Usas.Model.Cms.cms_block GetModel(int b_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" b_id,b_name,b_path,b_remark,b_setting_url ");
            strSql.Append(" from cms_block ");
            strSql.Append(" where b_id=" + b_id + " ");
            Usas.Model.Cms.cms_block model = new Usas.Model.Cms.cms_block();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["b_id"].ToString() != "")
                {
                    model.b_id = int.Parse(ds.Tables[0].Rows[0]["b_id"].ToString());
                }
                model.b_name = ds.Tables[0].Rows[0]["b_name"].ToString();
                model.b_path = ds.Tables[0].Rows[0]["b_path"].ToString();
                model.b_remark = ds.Tables[0].Rows[0]["b_remark"].ToString();
                model.b_setting_url = ds.Tables[0].Rows[0]["b_setting_url"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select b_id,b_name,b_path,b_remark,b_setting_url ");
            strSql.Append(" FROM cms_block ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" b_id,b_name,b_path,b_remark,b_setting_url ");
            strSql.Append(" FROM cms_block ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        */

        #endregion  成员方法
    }
}

